//**************************************************************************
// Copyright 2008 Peter Project (http://www.codeplex.com/peter)
//
// License : GNU General Public License (GPL)
// For full text of the license please goto:
// http://www.gnu.org/licenses/gpl-2.0.html
//
// This code is provided on an AS IS basis, with no WARRANTIES,
// CONDITIONS or GUARANTEES of any kind.
//
//		File: GListBox.cs
//
//**************************************************************************

using System.Drawing;
using System.Windows.Forms;

namespace Peter
{
    ///<summary>Graphical ListBox - Listbox that contains a graphic associate with each item in the list</summary>
    ///<remarks>This class is sealed to prevent possible virtual call in constructor</remarks>
    public sealed class GListBox : ListBox
    {
        private ImageList _myImageList;

        ///<summary>List of images from which to pull graphics for the list</summary>
        public ImageList ImageList
        {
            get { return _myImageList; }
            set { _myImageList = value; }
        }

        ///<summary>Default constructor</summary>
        public GListBox ()
        {
            // Set owner draw mode
            this.DrawMode = DrawMode.OwnerDrawFixed;
            this.BorderStyle = BorderStyle.FixedSingle;
        }

        protected override void OnDrawItem (System.Windows.Forms.DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            if (this.Items.Count > 0)
            {
                GListBoxItem item;
                Rectangle bounds = e.Bounds;
                Size imageSize = _myImageList.ImageSize;
                try
                {
                    item = (GListBoxItem)Items[e.Index];
                    if (item.ImageIndex != -1)
                    {
                        ImageList.Draw(e.Graphics, bounds.Left, bounds.Top, item.ImageIndex);
                        e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
                            bounds.Left + imageSize.Width, bounds.Top);
                    }
                    else
                    {
                        e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
                            bounds.Left, bounds.Top);
                    }
                }
                catch
                {
                    if (e.Index != -1)
                    {
                        e.Graphics.DrawString(Items[e.Index].ToString(), e.Font,
                            new SolidBrush(e.ForeColor), bounds.Left, bounds.Top);
                    }
                    else
                    {
                        e.Graphics.DrawString(Text, e.Font, new SolidBrush(e.ForeColor),
                            bounds.Left, bounds.Top);
                    }
                }
            }
            base.OnDrawItem(e);
        }
    }

    ///<summary>Represents an item in a GListBox that has text and associated graphic</summary>
    public class GListBoxItem
    {
        #region Member variables
        private string _myText;
        private int _myImageIndex;
        #endregion

        #region Properties
        ///<summary>Text for item</summary>
        public string Text
        {
            get { return _myText; }
            set { _myText = value; }
        }
        ///<summary>Identifier for item graphic</summary>
        public int ImageIndex
        {
            get { return _myImageIndex; }
            set { _myImageIndex = value; }
        }
        #endregion

        /// <summary>
        /// Constructor for item providing text and graphic </summary>
        /// <param name="text">string representing text for item</param>
        /// <param name="index">Identifier for item graphic</param>
        public GListBoxItem (string text, int index)
        {
            _myText = text;
            _myImageIndex = index;
        }
        ///<summary>Constructor that provides text for item</summary>
        ///<param name="text">string representing text for item</param>
        public GListBoxItem (string text) : this(text, -1) { }

        ///<summary>Default constructor - empty text, no graphic</summary>
        public GListBoxItem () : this("") { }

        ///<summary>
        ///Returns a <see cref="T:System.String"></see> that represents the current <see cref="T:Peter.GListBoxItem"></see>.
        ///</summary>
        ///
        ///<returns>
        ///A <see cref="T:System.String"></see> that represents the current <see cref="T:Peter.GListBoxItem"></see>.
        ///</returns>
        public override string ToString ()
        {
            return _myText;
        }
    }
}
